package com.penfei.springrun

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.support.config.FastJsonConfig
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.http.HttpMessageConverters

import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType
import org.springframework.http.converter.HttpMessageConverter

/**
 * Hello world!
 */
@SpringBootApplication
open class SpringRunApplication {
    /**
     *
     * spring boot默认的jackson，将jackson换成fastjson进行json解析
     */
    @Bean
    open fun fastJsonHttpMessageConverters(): HttpMessageConverters {
        // 1、需要先定义一个convert 转换消息的对象
        val fastConverter = FastJsonHttpMessageConverter()

        // 2、添加fastJson 的配置信息，比如：是否需要格式化返回的json数据
        val fastJsonConfig = FastJsonConfig()
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat)
        // 处理中文乱码
        val fastMediaTypes: MutableList<MediaType> = ArrayList()
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8)
        fastConverter.supportedMediaTypes = fastMediaTypes

        // 3、在convert中添加配置信息
        fastConverter.fastJsonConfig = fastJsonConfig
        val converter: HttpMessageConverter<*> = fastConverter
        return HttpMessageConverters(converter)
    }
}

fun main(args: Array<String>) {
    SpringApplication.run(SpringRunApplication::class.java, *args)
}